﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace WebSite.Admin.Core
{

    /// <summary>
    ///AdminBasePage 的摘要说明
    /// </summary>
    public class AdminBasePage : Page
    {

        #region 参数


        #endregion

        #region 事件
        //protected override void OnInit( EventArgs e )
        //{
        //    base.OnInit( e );
        //}

        protected override void OnLoad(EventArgs e)
        {
            if (string.IsNullOrEmpty( Session["AdminUser"] as string ) )
            {
                Response.Redirect( "/admin/adminlogin.aspx" );
            }

            base.OnLoad(e);
        }

        #endregion


        #region 跳转提示页面

        /// <summary>
        /// 提示信息，页面Back
        /// </summary>
        /// <param name="message"></param>
        public void AlertMessageBack( string message )
        {
            this.AlertMessage( message, string.Empty, true );
        }
        /// <summary>
        /// 提示信息，页面不back
        /// </summary>
        /// <param name="message"></param>
        public void AlertMessage( string message )
        {
            this.AlertMessage( message, string.Empty, false );
        }
        /// <summary>
        /// 提示信息，页面重定向
        /// </summary>
        /// <param name="message"></param>
        /// <param name="url"></param>
        public void AlertMessage( string message, string url )
        {
            this.AlertMessage( message, url, false );
        }
        /// <summary>
        /// 提示信息，可以重定向，可以back
        /// </summary>
        /// <param name="message"></param>
        /// <param name="Url"></param>
        /// <param name="back"></param>
        public void AlertMessage( string message, string Url, bool back )
        {
            string alert = string.Format( @"<script>alert('" + message.Replace( "'", "\'" ) + "');" );
            if (back)
            {
                alert += "history.back();";
            }
            else if (!string.IsNullOrEmpty( Url ))
            {
                alert += "window.location.href='" + Url + "';";
            }
            alert += "</script>";

            HttpContext.Current.Response.Write( alert );
            HttpContext.Current.Response.End();
        }


        public void AlertMessage( string message, string Url, bool back, bool IsTop )
        {
            string pageLayer = string.Empty;
            if (IsTop)
            {
                pageLayer = "top";
            }
            else
            {
                pageLayer = "window";
            }

            string alert = string.Format( @"<script>alert('" + message.Replace( "'", "\'" ) + "');" );
            if (back)
            {
                alert += "history.back();";
            }
            else if (!string.IsNullOrEmpty( Url ))
            {
                alert += pageLayer + ".location.href='" + Url + "';";
            }
            alert += "</script>";

            HttpContext.Current.Response.Write( alert );
            HttpContext.Current.Response.End();
        }

        public void ServerRedirect( string Url, bool back, bool IsTop )
        {
            string pageLayer = string.Empty;
            if (IsTop)
            {
                pageLayer = "top";
            }
            else
            {
                pageLayer = "window";
            }

            string alert = string.Format( @"<script>" );
            if (back)
            {
                alert += "history.back();";
            }
            else if (!string.IsNullOrEmpty( Url ))
            {
                alert += pageLayer + ".location.href='" + Url + "';";
            }
            alert += "</script>";

            HttpContext.Current.Response.Write( alert );
            HttpContext.Current.Response.End();
        }

        public void SetSelectedIndexByText( System.Web.UI.HtmlControls.HtmlSelect ctrl, string text )
        {
            foreach (ListItem i in ctrl.Items)
            {
                if (i.Text == text)
                {
                    ctrl.SelectedIndex = -1;
                    i.Selected = true;
                    break;
                }
            }
        }

        public void SetSelectedIndexByValue( System.Web.UI.HtmlControls.HtmlSelect ctrl, string value )
        {
            foreach (ListItem i in ctrl.Items)
            {
                if (i.Value == value)
                {
                    ctrl.SelectedIndex = -1;
                    i.Selected = true;
                    break;
                }
            }
        }

        //public static void ShowMessage(string message)
        //{
        //    ShowMessage(message, string.Empty, true, null);
        //}
        //public static void ShowMessage(string message, bool back, params string[] para)
        //{
        //    ShowMessage(message, string.Empty, back, para);
        //}
        /// <summary>
        /// 提示信息
        /// </summary>
        /// <param name="message">提示文字，可用占位符</param>
        /// <param name="Url"></param>
        /// <param name="back"></param>
        /// <param name="para">占位符</param>
        //        public void ShowMessage(string message, string Url, bool back, params string[] para)
        //        {

        //            Session["MessageNotify"] = para == null ? message : string.Format(message, para);// code;// MessageSetting.GetMessage(code, para);


        //            if (string.IsNullOrEmpty(Url))
        //            {
        //                Url = Request.QueryString["returnurl"];
        //            }

        //            if (!string.IsNullOrEmpty(Url))
        //            {
        //                Session["MessageNotify"] += @"
        //<script type=""text/javascript"">setTimeout(function(){window.location.href='" + Url + "';},3000);</script>";
        //            }
        //            else
        //            {
        //                if (back)
        //                {
        //                    Session["MessageNotify"] += @"<script type=""text/javascript"">setTimeout(function(){window.history.back();},3000);</script>";
        //                }
        //            }
        //            Response.Redirect("~/message.aspx", true);
        //        }
        #endregion
    }

}